#include "myface.h"
#include <stdio.h>

void face_init(void)
{

	Py_Initialize();
	PyObject *sys = PyImport_ImportModule("sys");
	PyObject *path = PyObject_GetAttrString(sys, "path");
	PyList_Append(path, PyUnicode_FromString("."));
}

void face_fin(void)
{
	Py_Finalize();
}

double face_search()
{
	double rvalue = 0.0;
	//system(Wget_shot);
	if (-1 == access(Get_image, F_OK))
	{
		return rvalue;
	}
	PyObject *file = PyImport_ImportModule("myface");
	if (file == NULL)
	{
		PyErr_Print();
		printf("load face failed\n");
		goto FAILED_MODULE;
	}
	PyObject *fc = PyObject_GetAttrString(file, "facecpe");
	if (fc == NULL)
	{
		PyErr_Print();
		printf("load facecpe failed!\n");
		goto FAILED_FUNC;
	}
	PyObject *res = PyObject_CallObject(fc, NULL);
	if (res == NULL)
	{
		PyErr_Print();
		printf("call fc failed!\n");
		goto FAILED_CALL;
	}

	PyArg_Parse(res, "d", &rvalue);
	//printf ("rvalue = %lf\n", rvalue);

FAILED_CALL:
	Py_DECREF(res);
FAILED_FUNC:
	Py_DECREF(fc);
FAILED_MODULE:
	Py_DECREF(file);

	return rvalue;
}
